package cn.edu.zut.project1.controller;

import cn.edu.zut.project1.dto.OrdersDTO;
import cn.edu.zut.project1.pojo.Orders;
import cn.edu.zut.project1.pojo.Result;
import cn.edu.zut.project1.pojo.User;
import cn.edu.zut.project1.service.OrdersService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;//提取请求中的参数值，并赋值给控制器方法的参数
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/orders")
public class OrdersController {
    private final OrdersService ordersService;

    public OrdersController(OrdersService ordersService) {
        this.ordersService = ordersService;
    }

    //1.创建订单（下单）
    @RequestMapping("/create")
    public Result createOrder(@Validated @RequestBody OrdersDTO orderDTO, HttpSession session){
        //1.从session中获取买家信息
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser == null){
            return Result.error("请先登录");
        }
        Integer buyerId = loginUser.getId();
        try{
            String orderNo = ordersService.createOrder(orderDTO, buyerId);
            return Result.success("下单成功", orderNo);  // 返回订单编号
        } catch (RuntimeException e){
            return Result.error(e.getMessage());
        }
    }
    //2.付款
    @RequestMapping("/pay")
    public Result payOrder(@RequestParam String orderNo){
        try{
            ordersService.payOrder(orderNo);
            return Result.success("付款成功");
        } catch (RuntimeException e){
            return Result.error(e.getMessage());
        }
    }

    //3.发货
    @RequestMapping("/ship")
    public Result shipOrder(@RequestBody String orderNo,HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser == null){
            return Result.error("请先登录");
        }
        Integer sellerId = loginUser.getId();
        try {
            ordersService.shipOrder(orderNo, sellerId);
            return Result.success("发货成功");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());

        }



    }
    //4.确认收货
    @RequestMapping("/confirm")
    public Result confirmOrder(@RequestParam String orderNo, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser == null){
            return Result.error("请先登录");
        }
        Integer buyerId = loginUser.getId();
        try {
            ordersService.confirmOrder(orderNo, buyerId);
            return Result.success("确认收货成功,交易完成");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    //5.取消订单
    @RequestMapping("/cancel")
    public Result cancelOrder(@RequestParam String orderNo, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser == null){
            return Result.error("请先登录");
        }
        Integer userId = loginUser.getId();
        try {
            ordersService.cancelOrder(orderNo, userId);
            return Result.success("取消订单成功");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    //6.查询我买的订单列表
    @RequestMapping("/my/buy")
    public Result getBuyerOrders(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser == null){
            return Result.error("请先登录");
        }
        List<Orders> orders = ordersService.getMyBuyerOrders(loginUser.getId());
        if (orders == null){
            return Result.error("您还没有订单");
        }
        return Result.success(orders.toString());
    }

    //7.查询我卖的订单列表

    @RequestMapping("/my/sell")
    public Result getSellerOrders(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser == null){
            return Result.error("请先登录");
        }
        List<Orders> orders = ordersService.getMySellerOrders(loginUser.getId());
        if(orders == null){
            return Result.error("没有订单");
        }
        return Result.success(orders.toString());
    }




}
